//
//  KYRegisterViewController.m
//  kuaiyi
//
//  Created by 杨亮 on 16/3/12.
//  Copyright © 2016年 kuaiyi. All rights reserved.
//

#import "KYRegisterViewController.h"
#import "KYUserProtocolController.h"
#import "KYLoginViewController.h"
#import "UIViewController+MMDrawerController.h"

#import <SMS_SDK/SMSSDK.h>

#import <SVProgressHUD.h>

@interface KYRegisterViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneNoTextField;
@property (weak, nonatomic) IBOutlet UITextField *checkNoTextField;
@property (weak, nonatomic) IBOutlet UIButton *getCheckNoBtn;
@property (weak, nonatomic) IBOutlet UITextField *inviteNoTextField;
@property (weak, nonatomic) IBOutlet UIButton *agreeBtn;
@property (weak, nonatomic) IBOutlet UIButton *nextBtn;
@property (weak, nonatomic) IBOutlet UIButton *directLoginBtn;
@property (weak, nonatomic) IBOutlet UILabel *countLabel;
@property (nonatomic, assign) int totleCount;
@end

@implementation KYRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.agreeBtn.layer.cornerRadius = self.agreeBtn.bounds.size.height/2;
    self.agreeBtn.layer.masksToBounds = YES;
    self.agreeBtn.selected  = YES;
    self.countLabel.hidden = YES;
}
//重写返回方法
- (void)backToLastVC{
    [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
    [self.navigationController popToRootViewControllerAnimated:NO];
}
#pragma mark - 获取验证码按钮点击
- (IBAction)getCheckBtnKlick:(id)sender {
    if (![self isMobileNo:self.phoneNoTextField.text]) {
        [SVProgressHUD showInfoWithStatus:@"请输入正确的手机号码"];
    }else{
        //获取验证码
        [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.phoneNoTextField.text zone:@"86" customIdentifier:nil result:^(NSError *error) {
            if (error) {
                [SVProgressHUD showInfoWithStatus:@"神马情况"];
            }else{
                self.getCheckNoBtn.hidden = YES;
                self.countLabel.hidden = NO;
                [self statrCount];
            }
        }];
    }
    
}
#pragma mark - 定时器
-(void)statrCount{
    self.totleCount = 60;
    //一定要开启定时器
    NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerFire:) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    [timer fire];
}
-(void)timerFire:(NSTimer *)timer{
    self.totleCount--;
    //修改倒计时标签现实内容
    self.countLabel.text=[NSString stringWithFormat:@"%d",self.totleCount];
    //当倒计时到0时，做需要的操作，比如验证码过期不能提交
    if(self.totleCount == 0){
        [timer invalidate];
        self.getCheckNoBtn.hidden = NO;
        self.countLabel.hidden = YES;
    }
}
#pragma mark - 正则匹配方法
-(BOOL)isRightNumberWith:(NSString *)number pattern:(NSString *)pattern{
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    if ([regextestmobile evaluateWithObject:number]) {
        return YES;
    }else{
        return NO;
    }

}
-(BOOL)isMobileNo:(NSString *)mobileNo{
    NSString *pattern = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    if ([regextestmobile evaluateWithObject:mobileNo]) {
        return YES;
    }else{
        return NO;
    }
}
#pragma mark - 同意协议按钮
- (IBAction)agreeBtnKlick:(id)sender {
//    if (self.agreeBtn.selected == YES) {
//        self.agreeBtn.selected = NO;
//    }else if (self.agreeBtn.selected == NO){
//        self.agreeBtn.selected = YES;
//    }
    self.agreeBtn.selected = self.agreeBtn.selected == YES ? NO:YES;
}
#pragma mark - 用户协议书
- (IBAction)userProtocelBtnKlick:(id)sender {
    KYUserProtocolController *userProtocolVC = [[KYUserProtocolController alloc]init];
    [self.navigationController pushViewController:userProtocolVC animated:YES];
}
#pragma mark - 下一步按钮点击
- (IBAction)nextBtnKlick:(id)sender {
    if (![self isMobileNo:self.phoneNoTextField.text]) {
        [SVProgressHUD showInfoWithStatus:@"请输入正确的手机号码"];
    }else
    if (![self isRightNumberWith:self.checkNoTextField.text pattern:@"\\d{4}"]) {
        [SVProgressHUD showInfoWithStatus:@"请输入正确的验证码"];
    }else
    if (self.agreeBtn.selected == NO) {
        [SVProgressHUD showInfoWithStatus:@"请勾选用户协议"];
    }else{
        [SMSSDK commitVerificationCode:self.checkNoTextField.text phoneNumber:self.phoneNoTextField.text zone:@"86" result:^(NSError *error) {
            if (error) {
                [SVProgressHUD showInfoWithStatus:@"验证码有误"];
            }else{
                [SVProgressHUD showInfoWithStatus:@"验证成功了,请填写个人信息"];
            }
        }];
    }

}
- (IBAction)directLoginBtnKlick:(id)sender {
    KYLoginViewController *loginVC = [[KYLoginViewController alloc]init];
    [self.navigationController pushViewController:loginVC animated:YES];
}


@end
